package cn.com.ccxe.core.util;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

import cn.com.ccxe.core.io.MirrorWrite;

public class Tool {

	public static final Log logger = LogFactory.getLog(Tool.class);
	public static final PatternCompiler compiler = new Perl5Compiler();
	private static final PatternMatcher matcher = new Perl5Matcher();
	private static final String regex = "(^[f,h]t.*://.*\\..*\\..{2,}|.*\\..*\\..{2,})";
	/**
	 * 把字符串加工成正则表达式，将输入字符串的空格、换行符替换成正则表达式中的“|”。
	 * 
	 * @param keywords
	 *            关键字组成字符串，以空格、换行为分隔
	 * @return
	 */
	public static String normalizedRole(String keywords) {
		if(!isEmpty(keywords)) {
			return "";
		}
		StringBuffer filter = new StringBuffer(".*(");
		String[] newKeyword = keywords.toString().split("\\s");

		for (String str : newKeyword) {
			if (isEmpty(str)) {
				filter.append(str + "|");
			}
		}
		filter.deleteCharAt((filter.length() - 1));
		filter.append(").*");
		return filter.toString();
	}

	private static boolean isEmpty(String keywords) {
		return !StringUtil.isEmpty(keywords);
	}

	/**
	 * 将字符串做标准处理，防止非标准格式的数据输入。
	 * @param urls
	 * @return 
	 */
	public static String normalizedURL(String[] urls) {
		if(urls == null || urls.length == 0) {
			return ""; 
		}
		StringBuffer str = new StringBuffer();
		for (String url : urls) {
			if (!isEmpty(url)) { 
				continue;
			}
			if(!matcher.matches(url, getRegex(regex))) {
				logger.info("不规则的链接地址 : "+url);
				continue;
			}
			if (!url.startsWith("http")) {
				url = "http://"+url;
			}
			if (isEmpty(url)) {
				str.append(url+"\n");
			}
		}
		if(!isEmpty(str.toString())) {
			return "";
		}
		str.deleteCharAt((str.length() - 1));
		return str.toString().trim();
	}

	/**
	 * 将字符串做标准处理，防止非标准格式的数据输入。
	 * @param urls
	 * @return
	 */
	public static String normalizedURL(String str) {
		if(!isEmpty(str)) {
			return "";
		}
		String[] urls = str.trim().split("\\s");
		return normalizedURL(urls);
	}
	
	/**
	 * 返回镜像的相对路径
	 * @param url
	 * @return
	 */
	public static String getMirrorFileByUrl(String url) {
		MirrorWrite mw = new MirrorWrite();
		File file = new File(mw.getPath(url));
//System.out.println("MW: "+file.getPath());		
		if(file.exists()) {
//logger.info("File is exists ? Is " +file.exists());
			return mw.getServerPath(url).replace("\\", "/");
		}
		return "";
	}
	
	public static Pattern getRegex(String regex) {
		try {
			return compiler.compile(regex,
					Perl5Compiler.CASE_INSENSITIVE_MASK);
		} catch (MalformedPatternException e) {
			e.printStackTrace();
		}
		return null;
	}
	
    public static int getDate() {
    	return (int) (System.currentTimeMillis()/60/1000);
    }
}
